package com.whfc.emp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.whfc.common.enums.EnableState;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.emp.dao.AppEmpBankMapper;
import com.whfc.emp.dto.AppEmpBankDTO;
import com.whfc.emp.entity.AppEmpBank;
import com.whfc.emp.param.AppEmpBankAddParam;
import com.whfc.emp.param.AppEmpBankEditParam;
import com.whfc.emp.param.EnableBankParam;
import com.whfc.emp.service.AppEmpBankService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClasssName AppEmpBankServiceImpl
 * @Description 人员银行账户
 * @Author hw
 * @Date 2020/12/31 9:45
 * @Version 1.0
 */
@DubboService(interfaceClass = AppEmpBankService.class, version = "1.0.0")
public class AppEmpBankServiceImpl implements AppEmpBankService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppEmpBankMapper appEmpBankMapper;

    @Override
    public List<AppEmpBankDTO> list(Integer empId) {
        logger.info("人员银行账户列表:empId:{}", empId);
        return appEmpBankMapper.selectBankDTOList(empId);
    }

    @Override
    public void add(AppEmpBankAddParam param) {
        logger.info("添加银行账户,param:{}", param);
        Integer empId = param.getEmpId();
        String bankCode = param.getBankCode();
        String bankName = param.getBankName();
        String bankNumber = param.getBankNumber();

        // 根据银行账号去重
        AppEmpBank appEmpBank = appEmpBankMapper.selectByBankNumber(bankNumber);
        if (appEmpBank != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该银行卡号已存在");
        }

        AppEmpBank record = new AppEmpBank();
        record.setEmpId(empId);
        record.setBankCode(bankCode);
        record.setBankNumber(bankNumber);
        record.setBankName(bankName);
        appEmpBankMapper.insertSelective(record);
    }

    @Override
    public void edit(AppEmpBankEditParam param) {
        logger.info("编辑银行账户,param:{}", JSONObject.toJSONString(param));
        Integer bankId = param.getBankId();
        String bankCode = param.getBankCode();
        String bankName = param.getBankName();
        String bankNumber = param.getBankNumber();

        // 根据银行账号去重
        AppEmpBank appEmpBank = appEmpBankMapper.selectByBankNumber(bankNumber);
        if (appEmpBank != null && !bankId.equals(appEmpBank.getId())) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该银行卡号已存在");
        }

        AppEmpBank record = new AppEmpBank();
        record.setId(bankId);
        record.setBankCode(bankCode);
        record.setBankNumber(bankNumber);
        record.setBankName(bankName);
        appEmpBankMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void enable(EnableBankParam param) {
        logger.info("启用禁用银行账户,param:{}", param);
        Integer bankId = param.getBankId();
        Integer enableFlag = param.getEnableFlag();
        AppEmpBank record = new AppEmpBank();
        record.setId(bankId);
        record.setEnableFlag(enableFlag);
        appEmpBankMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void del(Integer bankId) {
        logger.info("删除银行账户,bankId:{}", bankId);
        AppEmpBank appEmpBank = appEmpBankMapper.selectByPrimaryKey(bankId);
        if (appEmpBank == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该银行卡不存在");
        }
        if (EnableState.ENABLED.getValue().equals(appEmpBank.getEnableFlag())) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该银行卡为启用状态，不能删除");
        }
        appEmpBankMapper.deleteLogicById(bankId);
    }
}
